#include "updatedialog.h"
#include "common.h"

#include <QDebug>
//#include <DBlurEffectWidget>
//#include <DTitlebar>
//#include <DFontSizeManager>

#define DIALOG_WIDTH 385 //dialog  宽度
#define DIALOG_HEIGHT 145 //dialog  高度
#define LEFT_MARGINS 0 //左边距
#define TOP_MARGINS 0 //上边距
#define RIGHT_MARGINS 0 //右边距离
#define BUTTOM_MARGINS 0 //下边距离
#define SPACING 5 //布局间隙
#define PB_DELETE_WIDTH 185 //按钮宽度
#define PB_DELETE_HEIGHT 35 //按钮高度

#define SET_ACC_NAME(control, name) SET_ACCESS_NAME(control, updateDialog, name)

UpdateDialog::UpdateDialog(QWidget *parent)
    : QFrame(parent)
{
    setObjectName("UpdateDialog");
    this->resize(DIALOG_WIDTH, DIALOG_HEIGHT);
    this->setMinimumSize(QSize(DIALOG_WIDTH, DIALOG_HEIGHT));
    this->setMaximumSize(QSize(DIALOG_WIDTH, DIALOG_HEIGHT));
    //设置为Dialog
    setWindowFlags(Qt::Dialog);
    //设置为阻态
    setWindowModality(Qt::ApplicationModal);
    //背景透明化
//    setAttribute(Qt::WA_TranslucentBackground);
//    //模糊效果
//    DBlurEffectWidget *m_blurEffect = new DBlurEffectWidget(this);
//    SET_ACC_NAME(m_blurEffect, blurEffectWidget);
//    m_blurEffect->setBlendMode(DBlurEffectWidget::BehindWindowBlend);

//    //设置模糊大小
//    m_blurEffect->resize(size());
//    m_blurEffect->lower();

    m_cancle = new QPushButton(this);
    SET_ACC_NAME(m_cancle, cancleButton);
    m_update = new QPushButton(this);
    SET_ACC_NAME(m_update, updateButton);
    init();
    connect(m_cancle, SIGNAL(clicked()), this, SLOT(cancle()));
    connect(m_update, SIGNAL(clicked()), this, SLOT(update()));
}

UpdateDialog::~UpdateDialog()
{
    disconnect(this);
}

void UpdateDialog::init()
{
    //主布局
    QVBoxLayout *mainlayout = new QVBoxLayout(this);
    //边距离
    mainlayout->setContentsMargins(LEFT_MARGINS, TOP_MARGINS, RIGHT_MARGINS, BUTTOM_MARGINS + 10);
    mainlayout->setSpacing(SPACING);
    QHBoxLayout *titleBarLayout = new QHBoxLayout(this);
    titleBarLayout->setContentsMargins(5, TOP_MARGINS, RIGHT_MARGINS, BUTTOM_MARGINS);
    //关闭按钮
//    DTitlebar *titleBar = new DTitlebar(this);
//    SET_ACC_NAME(titleBar, closeTitleBar);
//    titleBar->setBackgroundTransparent(true);
//    titleBar->setTitle("");
//    titleBar->setFixedSize(40, 40);
//    titleBar->setMenuVisible(false);
    //titleBar->setIcon(QIcon::fromTheme("deepin-defender"));
    QLabel *label = new QLabel(this);
    SET_ACC_NAME(label, lable);
    label->setFixedSize(QSize(40, 40));
    label->setPixmap(QIcon::fromTheme("deepin-defender").pixmap(QSize(30, 30)));
    titleBarLayout->addWidget(label, 0, Qt::AlignHCenter | Qt::AlignVCenter);
    titleBarLayout->addStretch(1);
//    titleBarLayout->addWidget(titleBar, 0, Qt::AlignRight | Qt::AlignTop);
    mainlayout->addLayout(titleBarLayout);
    //内容
    m_tipLable = new QLabel(tr("New virus database available, update now?"), this);
    SET_ACC_NAME(m_tipLable, tipLable);
    m_tipLable->setAlignment(Qt::AlignCenter);
    m_tipLable->setWordWrap(true);
    mainlayout->addWidget(m_tipLable);

    QHBoxLayout *layout = new QHBoxLayout(this);
    layout->setContentsMargins(5, 5, 5, 5);
    layout->setSpacing(10);

    // 取消
    m_cancle->setText(tr("Cancel", "button"));
    m_cancle->setMinimumSize(QSize(PB_DELETE_WIDTH, PB_DELETE_HEIGHT));
    m_cancle->setMaximumSize(QSize(PB_DELETE_WIDTH, PB_DELETE_HEIGHT));
    // 后台更新
    m_update->setText(tr("Background Update"));
    m_update->setMinimumSize(QSize(PB_DELETE_WIDTH, PB_DELETE_HEIGHT));
    m_update->setMaximumSize(QSize(PB_DELETE_WIDTH, PB_DELETE_HEIGHT));
    layout->addWidget(m_cancle, 0, Qt::AlignVCenter | Qt::AlignLeft);
    layout->addWidget(m_update, 0, Qt::AlignVCenter | Qt::AlignLeft);
    mainlayout->addLayout(layout);
}

// 取消按钮点击
void UpdateDialog::cancle()
{
    hide();
    deleteLater();
}

// 更新按钮点击
void UpdateDialog::update()
{
    hide();
    Q_EMIT notifyUpdateVdb();
    deleteLater();
}
